//
//  HMVisitorLoginView.swift
//  WeiboTT
//
//  Created by 王嘉涛 on 2016/11/13.
//  Copyright © 2016年 王嘉涛. All rights reserved.
//

import UIKit
import SnapKit

//swift中使用代理
@objc protocol HMVisitorLoginViewDelegate: NSObjectProtocol {
    //在协议中定义方法的声明 是必选的协议方法 默认必须实现,在swift中不实现就默认报错,
    
    //用户将要登录
    @objc optional func userWillLogin()
    
    //用户将要注册
    func userWillRegister()
}

class HMVisitorLoginView: UIView {
    
    //声明属性  主要需要使用weak关键字来修饰
    weak var delegate: HMVisitorLoginViewDelegate?
    
    //MARK: 外界设置界面信息的方法
    func updateInfo(title: String, imageName: String,isHome: Bool = false) {
        //修改文字
        self.tipLabel.text = title
        //修改圈圈的图片
        self.circleView.image = UIImage(named: imageName)
        if isHome {
            //执行动画
            startAnimation()
        } else {
            //不是首页
            self.largeHouse.isHidden = true
            //隐藏遮罩
            self.backView.isHidden = true
        }
    }
    
    //动画效果
    private func startAnimation() {
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        //设置属性
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        animator.duration = 20
        //isRemovedOnCompletion: 当动画效果啊完成之后就从图层移除,设置为false 就不会被移除图层, 默认值值是true
        animator.isRemovedOnCompletion = false
        //将动画添加到circleView的图层上
        circleView.layer.add(animator, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func loginBtnDidClick() {
        //调用代理方法 可选的协议方法
        delegate?.userWillLogin?()
    }
    
    @objc private func registerBtnDidClick() {
        //必选的协议方法
        delegate?.userWillRegister()
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        
        //监听按钮的点击事件
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //SnapKit 也是由Masonry团队为 Swift 出的自动布局的框架
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        
        largeHouse.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
            //make.width.equalTo(220)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    
    //实例化子控件
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //圈圈
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "关注一些人,回到这里看看有什么惊喜", fontSize: 14, textColor: UIColor.darkGray)
        //设置行数 + 约束宽度
        l.numberOfLines = 0
        l.textAlignment = .center
        //以后设置文字换行,不要使用设置左右边距来限制宽度,不要直接设置约束宽度
        //设置左右: 在计算非常精确的文本自动换行的时候 会导致高度计算的不准确
        l.preferredMaxLayoutWidth = 220
        return l
    }()
    //登录
    lazy var loginBtn: UIButton = {
        let btn = UIButton(title: "登录", fontSize: 14, textColor: UIColor.darkGray)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    //注册
    lazy var registerBtn: UIButton = {
        let btn = UIButton(title: "注册", fontSize: 14, textColor: UIColor.orange)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
}





